package com.company.admin.service.job.social;


import com.company.admin.common.myException.BizException;
import com.company.admin.common.util.StrAndListUtil;
import com.company.admin.dao.file.FileDao;
import com.company.admin.dao.job.social.SocialJobDao;
import com.company.admin.domain.job.social.SocialJobDomain;
import com.company.admin.entity.file.AttachFileEntity;
import com.company.admin.entity.job.social.SocialJobEntity;
import com.company.admin.req.job.social.SocialJobAddRequest;
import com.company.admin.req.job.social.SocialJobDeletRequest;
import com.company.admin.req.job.social.SocialJobListRequest;
import com.company.admin.req.job.social.SocialJobModifyRequest;
import com.company.admin.resp.job.social.SocialJobListResponse;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @program: myProject
 * @description: ioc 容器中装的bean，有一些不是对象的本身，而是通过动态代理生成的代理对象（一些通过aop增强的类，比如有@transactional的修饰）
 * 如果实现的有接口，spring会默认使用jdk的动态代理的方式，这时候再自动加载到ioc容器的时候，会报错的
 * @author: zhaochenglin
 * @create: 2019-03-03 14:58
 **/
@Service
public class SocialJobService {

    @Autowired
    private SocialJobDao socialJobDao;
    @Autowired
    private FileDao fileDao;

    @Transactional
    public void add(SocialJobAddRequest socialJobAddRequest){
        checkAdd(socialJobAddRequest);
        SocialJobEntity entity = new SocialJobEntity();
        entity.setCreateTime(new Date());
        entity.setSocialJobAddress(socialJobAddRequest.getSocialJobAddress());
        entity.setSocialJobInfo(socialJobAddRequest.getSocialJobInfo());
        entity.setSocialJobName(socialJobAddRequest.getSocialJobName());
        entity.setUpdateTime(new Date());
        List<Long> ids = socialJobAddRequest.getSocialJobPictureIds();
        String pictureIds = StrAndListUtil.listToStr(ids);
        entity.setSocialJobPicture(pictureIds);
        socialJobDao.insert(entity);

    }
    private void checkAdd(SocialJobAddRequest request){
        if (request == null){
            throw new BizException(208,"没有请求的实体");
        }
        if (StringUtils.isBlank(request.getSocialJobName())){
            throw new BizException(208,"工作名称不可以为空");
        }
        if (StringUtils.isBlank(request.getSocialJobAddress())){
            throw new BizException(208,"工作的地址不可以为空");
        }
        if (StringUtils.isBlank(request.getSocialJobInfo())){
            throw new BizException(208,"工作详情不可以为空");
        }
    }

    @Transactional
    public void modify(SocialJobModifyRequest request){
        checkModify(request);
        SocialJobEntity entity = new SocialJobEntity();
        entity.setSocialJobPicture(StrAndListUtil.listToStr(request.getSocialJobPictureIds()));
        entity.setUpdateTime(new Date());
        entity.setSocialJobName(request.getSocialJobName());
        entity.setSocialJobInfo(request.getSocialJobInfo());
        entity.setSocialJobAddress(request.getSocialJobAddress());
        entity.setSocialJobId(request.getSocialJobId());
        socialJobDao.modify(entity);
    }

    private void checkModify(SocialJobModifyRequest request){
        if (request.getSocialJobId() == null){
            throw new BizException(208,"请选择你要编辑的社会岗位");
        }
    }

    public SocialJobListResponse list(SocialJobListRequest request){
        if (request.getLength() == null || request.getOffset() == null){
            throw new BizException(208,"分页参数不完整");
        }
        SocialJobListResponse response = new SocialJobListResponse();
        Long totalCount = socialJobDao.totalCount();
        if (totalCount.equals(0L)){
            response.setTotalCount(totalCount);
            return response;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("offset", request.getOffset());
        param.put("length", request.getLength());
        List<SocialJobEntity> socialJobEntities = socialJobDao.list(param);
        List<SocialJobDomain> domains = new ArrayList<>();
        for (SocialJobEntity entity : socialJobEntities){
            SocialJobDomain domain = new SocialJobDomain();
            String pictureIds = entity.getSocialJobPicture();
            List<Long> ids = StrAndListUtil.strToList(pictureIds);
            List<AttachFileEntity> attachFileEntities = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(ids)){
                attachFileEntities = fileDao.selectByIds(ids);
            }

            domain.setAttachFileEntities(attachFileEntities);
            domain.setCreateTime(entity.getCreateTime());
            domain.setSocialJobAddress(entity.getSocialJobAddress());
            domain.setSocialJobId(entity.getSocialJobId());
            domain.setSocialJobInfo(entity.getSocialJobInfo());
            domain.setSocialJobName(entity.getSocialJobName());
            domain.setUpdateTime(entity.getUpdateTime());
            domains.add(domain);
        }
        response.setTotalCount(totalCount);
        response.setSocialJobDomains(domains);
        return response;
    }

    @Transactional
    public void delete(SocialJobDeletRequest request){
        if (CollectionUtils.isEmpty(request.getSocialJobIds())){
            throw new BizException(208,"请选择要删除的社会岗位");
        }

        socialJobDao.delete(request.getSocialJobIds());
    }
}
